<!DOCTYPE html>
<html>
<head>
<div th:include="base-libs :: base-libs" />
<title>号源规则管理</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">

<style>
	.spanBtn{
		font-weight: bold;
    	color: blue;
    	cursor:pointer;
	}
</style>

<script th:inline="javascript">
	/**读取request中的初始化数据*/
	var requestData = [[${requestData}]];
</script>
<script>
	$(function(){
    	initView();
    	initData();
    	$("#btnQuery").trigger("click");
    })
    
    function initData(){
        //根据权限，隐藏不能使用的查询条件
        if(!requestData["isManager"]){
            var value = {PTJGID: parent.userInfo.PTJGID,YLJGMC: parent.userInfo.YLJGMC};
            $("#combogridYljg").combogrid("setValue", value);
            $(".hidden").hide();
        }
	}
    	
    function initView() {
    	//回车查询
    	enterToQuery(btnQueryHandler);

        /** 医疗机构combogrid */
        var readonly = !requestData["isManager"];
        $("#combogridYljg").combogrid({
            height: 30
            ,width: 300
            ,panelWidth: 500
            ,singleSelect: true
            ,pagination: true
            ,idField: 'PTJGID'
            ,textField: 'YLJGMC'
            ,mode: "remote"
            ,readonly: readonly
            ,hasDownArrow: !readonly
            ,url: "/console/yljgxx/queryPageSummary.service"
            ,delay: 1000
            ,reversed: false
            ,fitColumns: true
            ,columns:[[
                {field:'YLJGMC',title:'机构名称',width:240},
                {field:'XZQHMC',title:'行政区划',width:80},
                {field:'WSJGLBMC',title:'机构类别',width:90},
                {field:'YYDJMC',title:'机构等级',width:80}
            ]]
            ,onBeforeLoad: function(queryParams) {
                queryParams["pageSize"] = queryParams["rows"];
                queryParams["pageNumber"] = queryParams["page"];
                queryParams["YLJGMC"] = queryParams["q"];
            },
        });
    	
		$("#hygzTb").datagrid({
				idField : "GZBH",
				striped : true,
				clickToSelect: true,
				fitColumns: true,
				fit:true,
				scrollbarSize:15,
				singleSelect: true,
				title: '规则列表',
				columns : [
				   [
// 						{field : "xz",checkbox:true,rowspan:2},
                        {field : "YLJGMC",title : "医疗机构名称",width:80, align:'center',rowspan:2},
						{field : "GZMC",title : "规则名称",width:80, align:'center',rowspan:2},
						{field : "KHBL",title : "控号比例(%)",width:60, align:'center',rowspan:2},
						{field : "KHDXLX",title : "放号对象",width:50, align:'center',rowspan:2,formatter:function(value){
							var obj = {'01':'科室'};
							return obj[value] || value;
						}},
						{field : "SJGZSZ",title : "时间规则设置", align:'center', colspan:3},
						{field : "SFYSHY",title : "优势<br/>号源",width:30, align:'center',rowspan:2, formatter:function(value){
							var obj = {'0':'否','1':'是'};
							return obj[value] || value;
						}},
						{field : "CJSJ",title : "创建时间",width:48, align:'center',rowspan:2,formatter:function(value){
							if(!StringUtils.isEmpty(value)){
								return value.substring(0,10);
							}
						}},
                        {field : "ZT",title : "状态",width:50, align:'center',rowspan:2, formatter:function(value){
                            var ztObj = {'1':'启用','2':'停用','3':'作废'};
                            return ztObj[value] || value;
                        }},
						{field : "CZ",title : "操作",width:95, align:'center',rowspan:2, formatter:function(value,row){
							var rowCopy = JSON.stringify(row).replace(/"/g, '&quot;');
							var xg = '<span class="spanBtn" onclick="toAddView('+ rowCopy +')">修改</span>';
							var sc = '<span class="spanBtn" onclick="deleteHcgz('+ rowCopy +')">删除</span>';
							var ty = '<span class="spanBtn" onclick="freezeHcgz('+ rowCopy +',2)">停用</span>';
                            var qy = '<span class="spanBtn" onclick="freezeHcgz('+ rowCopy +',1)">启用</span>';
							var gljg='<span class="spanBtn" onclick="addGljg('+ rowCopy +')">关联机构</span>';
							var result;
							if(row.ZT == 1){
								return ty + "&nbsp;&nbsp" + xg+ "&nbsp;&nbsp" + sc + "&nbsp;&nbsp" + gljg;
							}else if(row.ZT == 2){
								return qy + "&nbsp;&nbsp" + xg + "&nbsp;&nbsp" + sc;
							}
						}},
				 ],
				 [
						{field : "FHTS",title : "放号<br/>天数",width:30, align:'center',rowspan:1,formatter:function(val,row){
							return parseFloat(row.PTTS) + parseFloat(row.YSTS) || '';
						}},
						{field : "PTTS",title : "普通<br/>天数",width:30, align:'center',rowspan:1},
						{field : "YSTS",title : "优势<br/>天数",width:30, align:'center',rowspan:1},
				 ]],
				pagination : true,
				pageList: [10, 20, 50, 100, 200],
				method : "post",
				onClickRow : function(rowIndex, rowData){
					queryGljg(rowIndex);
	            },
				onBeforeLoad : function(params) {
                    params.PTJGID = $("#combogridYljg").combobox("getValue");
                    getContainerDataByClassName('conditionBox','condition',params);//查询条件
				},
				onLoadSuccess: function(data){
                    loadRowIndex = -1;
 				},
		});

		$("#gljgTb").datagrid({
			idField : "YLJGDM",
			striped : true,
			fitColumns: true,
			fit:true,
			scrollbarSize:15,
			rownumbers: true,
			singleSelect: true,
			title: '关联机构',
			columns : [[
					{field : "YLJGMC",title : "机构名称",width:80, align:'center'},
			 ]],
			onBeforeLoad : function(params) {
				return false;
			},
			onLoadSuccess: function(data){
//				alert(JSON.stringify(data))
			}
		});


	}

   	/** 查询  */
   	function btnQueryHandler(){
   		$("#hygzTb").datagrid({"url": "queryPage.service"});
   	}
   	
   	/** 跳转到新增页面  */
   	function toAddView(row){
   		StoreCache.setCache('hygzglData',row);
   		$.QuickUrlUtils.redirectUrl("manager.html");
   	}
   	
   	/** 删除号源规则  */
   	function deleteHcgz(row){
   	    var delTip = "删除后不可恢复，是否继续？";
   	    if(!StringUtils.isEmpty(row.JGS)){
   	        delTip = "该规则已关联医疗机构，删除后不可恢复，是否继续?";
		}
   		$.QuickAlert.alertConfirm({
			content : delTip,
			callback : function() {
				$.QuickRemote.AjaxJson("save", deleteHandler, {type:'update', GZBH: row.GZBH, ZT:3});
			}
		})
   	}
   	/** 停用/启用 号源规则 */
   	function freezeHcgz(row,ZT){
        var ztObj = {'1':'启用','2':'停用'};
        $.QuickAlert.alertConfirm({
            content : "确认"+ztObj[ZT]+"该规则？",
            callback : function() {
				$.QuickRemote.AjaxJson("save", deleteHandler, {type:'update', GZBH: row.GZBH, ZT: ZT},ZT);
            }
        })
   	}
   	
   	/** 添加关联机构  */
   	function addGljg(row){
   		StoreCache.setCache('hygzglData',row);
   		parent.layer.open({
			  type: 2,
			  area: ["50%", "60%"],
			  content: "/"+contextRoot + 'hygzgl/managerGljg.html',
			  scrollbar: false,
			  resize: false,
			  title:'添加关联机构',
			  end: function(){
				  var refresh = StoreCache.removeCache("refresh"); //是否刷新关联机构列表
				  if(refresh){
					  queryGljg(-1);//重新加载管理机构
				  }
			  }
		});
   	}
   	
   	/** 删除回调 */
	function deleteHandler(result,ZT){
		if(result.state){
            var tip = result.info;
            if(ZT==1) tip = '您已启用该规则！';
            if(ZT==2) tip = '您已停用该规则！';
			$.QuickAlert.alertNormal('操作成功！', {callback: function(){
				$("#btnQuery").trigger("click");
			}});
		}else{
			$.QuickAlert.alertFail({content: result.info});
		}
	}
   	
   	/** 查询某规则的关联机构  */
   	var loadRowIndex;
   	function queryGljg(rowIndex){
   		if(rowIndex != loadRowIndex){
			var select = $("#hygzTb").datagrid('getSelected');
			$("#gljgTb").datagrid({
				"url": "queryGljg.service",
				onBeforeLoad : function(params) {
					if(select.GZBH){
						params.GZBH = select.GZBH;
					}else{
//					    alert()
						return false;
					}
				},
			})
	   		loadRowIndex = rowIndex;
   		}
   	}
</script>

</head>

<body>
	<div class="easyui-layout" fit="true" border="false">
		<div region="north" style="overflow:hidden;padding:15px 0px;background:#f9f9f9" border="false">
			<form action="downloadWdqyhz.service" method="post" enctype="multipart/form-data" id="downloadForm" >
				<table class="form-table" id="conditionBox">
					<tr>
						<td class="td-text hidden">医疗机构名称：</td>
						<td class="td-content hidden">
							<input id="combogridYljg"/>
						</td>
						<td class="td-text">规则名称：</td>
						<td class="td-content">
							<input name="GZMC" class="form-control condition"  style="width: 100%;">
						</td>
						<td class="td-text" style="text-align:center;width:280px">
							<button id="btnQuery" class="btn btn-info" type="button" onclick="btnQueryHandler()">查询</button>
							<button type="button" onclick="toAddView()">新增</button>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div region="center">
			<table id="hygzTb" border="false"></table>
		</div>
		<div region="east" style="width: 250px;margin-left: 10px;">
				<table id="gljgTb" border="false"></table>
		</div>
	</div>
	
</body>
</html>
